﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

namespace JsRunTool
{
    public class alldata
    {

    }

    public class MyAdo
    {
        public static string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\DB.mdb";
        public OleDbConnection cnn = new OleDbConnection(connString);
        public string UserName = "";

        public void CloseCnn()
        {
            if (cnn.State.ToString().ToLower() != "closed")
            {
                cnn.Close();
            }
        }

        public void OpenCnn()
        {
            if (cnn.State.ToString().ToLower() != "closed")
            {
                cnn.Close();
            }
            cnn.Open();
        }

        /// <summary>
        /// 执行插入语句,更新语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int exec(string sql)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand(sql, cnn);
                OpenCnn();
                int i = cmd.ExecuteNonQuery();
                return i;
//                 if (i != 0)
//                 {
//                     MessageBox.Show("成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//                 }
//                 else
//                 {
//                     MessageBox.Show("失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//                 }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                
            }
            finally
            {
                cnn.Close();
            }
            return 0;
        }

        public string[] getDt()
        {
            DataTable dt = new DataTable();
            try
            {
                string varTableName = "data";
                OpenCnn();
                dt = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, varTableName, null });
                int n = dt.Rows.Count;
                string[] strTable = new string[n];
                int m = dt.Columns.IndexOf("COLUMN_NAME");

                DataView dataView = dt.DefaultView;
                dataView.Sort = "ordinal_position asc";
                dt = dataView.ToTable();

                for (int i = 0; i < n; i++)
                {
                    DataRow m_DataRow = dt.Rows[i];
                    strTable[i] = m_DataRow.ItemArray.GetValue(m).ToString();
                }
                return strTable;
            }
            catch (Exception e)
            {
                string msg = e.Message;
                return null;
            }
            finally
            {
                cnn.Close();
            }
        }

        public string[] getOrderno()
        {
            OpenCnn();
            string sql = "select distinct orderno as  orderno from data";
            DataTable dt = getDataTb(sql);
            string[] strOrderno = new string[dt.Rows.Count];
            for (int i = 0; i<dt.Rows.Count;i++ )
            {
                strOrderno[i] = dt.Rows[i][0].ToString();
            }
            return strOrderno;
        }

        public OleDbDataReader getDataReader(string sql)
        {
            OpenCnn();
            OleDbCommand cmd = new OleDbCommand(sql, cnn);
            OleDbDataReader dr = cmd.ExecuteReader();

            return dr;
        }

        public DataTable getDataTb(string sql)
        {
            DataTable dt = new DataTable();
            OpenCnn();
            OleDbCommand cmd = new OleDbCommand(sql, cnn);

            OleDbDataAdapter adr = new OleDbDataAdapter();
            adr.SelectCommand = cmd;
            adr.Fill(dt);

            return dt;
        }
    }
}
